package map;
import java.sql.*;
import java.util.*;

import dm.Editorial;


public class EditorialDAO {
	
    public void AddEditorial(String nro, String nombre) throws SQLException
    {
       	String sentencia = "insert into publishers(pub_id, pub_name) values(?,?)";
		PreparedStatement pstmt = new ConfigDAO().GetPrepareCall(sentencia);
		pstmt.setString(1, nro);
		pstmt.setString(2, nombre);
		pstmt.executeUpdate();
		pstmt.getConnection().close();

    }
    
    public void DelEditorial(String nro) throws SQLException
    {
       	String sentencia = "delete publishers where pub_id = ?";
		PreparedStatement pstmt = new ConfigDAO().GetPrepareCall(sentencia);
		pstmt.setString(1, nro);
		pstmt.executeUpdate();
	    pstmt.getConnection().close();
    }
    
    public Editorial GetEditorial(String nro) throws SQLException
    {
       	String sentencia = "select * from publishers where pub_id = ?";
		PreparedStatement pstmt = new ConfigDAO().GetPrepareCall(sentencia);
		Editorial editorial = null;
		pstmt.setString(1, nro);
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			editorial = MapEditorial(rs);
		}
	    pstmt.getConnection().close();
		return editorial;
    }
    
	public Set<Editorial> GetEditoriales() throws SQLException 
	{
       	String sentencia = "select * from publishers";
		PreparedStatement cstmt = new ConfigDAO().GetPrepareCall(sentencia);
		Set<Editorial> editoriales = new HashSet<Editorial>();
		ResultSet rst = cstmt.executeQuery();
		while (rst.next())
		{
			editoriales.add(MapEditorial(rst));
		}
		cstmt.getConnection().close();
		return editoriales;
	}
	
	Editorial MapEditorial(ResultSet rs) throws SQLException {
		Editorial editorial = new Editorial();
		editorial.setNumero(rs.getString("pub_id"));
		editorial.setNombre(rs.getString("pub_name"));
		return editorial;
	}
}
